package site.wanjiahao;

import site.wanjiahao.list.LinkedList;
import site.wanjiahao.list.List;
import site.wanjiahao.stack.Stack;

// 队列使用双向链表实现
public class Queue1<E> {

    // 进入队列存放栈
    private final Stack<E> inStack = new Stack<>();

    // 出队列存放栈
    private final Stack<E> outStack = new Stack<E>();

    public int size() {
        return inStack.size() + outStack.size();
    }

    public boolean isEmpty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }

    public void clear() {
        inStack.clear();
        outStack.clear();
    }

    // 入队
    public void enQueue(E e) {
        inStack.push(e);
    }

    // 出队
    public E deQueue() {
        //  入栈对象 --> 出栈对象
        if (outStack.isEmpty()) {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
        }
        return outStack.pop();
    }

    // 获取对头元素
    public E front() {
        //  入栈对象 --> 出栈对象
        if (outStack.isEmpty()) {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
        }
        return outStack.peek();
    }
}
